<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../../../assets/docs.css" rel="stylesheet" type="text/css" media="screen" />
<title>MooTools 1.3 参考文档 - Utilities/Cookie</title>
</head>

<body>
  <div id="docs" class="doc">
    <div class="methods">
      <h4 class="menu-item"><a href="#Cookie">Cookie</a></h4>
      <div class="menu-item"><a href="#Cookie:write">write</a></div>
      <div class="menu-item"><a href="#Cookie:read">read</a></div>
      <div class="menu-item"><a href="#Cookie:dispose">dispose</a></div>
    </div>
    <div class="l-border r-border span-15 content">
      <h1 id="Cookie"><a href="#Cookie">Object: Cookie</a></h1>
      <p class="description">读/写 cookies.</p>
      <h3>荣誉:</h3>
      <ul>
        <li>Based on the functions by Peter-Paul Koch <a href="http://www.quirksmode.org">QuirksMode</a>.</li>
      </ul>
      <h3 id="Cookie-options">选项:</h3>
      <ul>
        <li>domain   - (<em>string</em>: 默认值为 false) Cookie 所属域名.</li>
        <li>path     - (<em>string</em>: 默认值为 '/') Cookie 所属路径.</li>
        <li>duration - (<em>number</em>: 默认值为 false) Cookie 过期天数. 若设置为 false 或 0 则 Cookie 与 session 同时过期, 即关闭浏览器时过期.</li>
        <li>secure   - (<em>boolean</em>: 默认值为 false) Stored cookie information can be accessed only from a secure environment.</li>
      </ul>
      <h3>提示:</h3>
      <ul>
        <li>In order to share the Cookie with pages located in a different path, the <a href="#Cookie-options">Cookie.options.domain</a> value must be set.</li>
      </ul>
      <h2 id="Cookie:write"><a href="#Cookie:write">Cookie 方法: write</a></h2>
      <p class="description">向浏览器写入 Cookie.</p>
      <h3>语法: </h3>
      <pre class="javascript"><span class="kw2">var</span> myCookie = Cookie.<span class="kw1">write</span><span class="br0">(</span>key, value<span class="br0">[</span>, options<span class="br0">]</span><span class="br0">)</span>;

</pre>
      <h3>参数: </h3>
      <ol>
        <li>key     - (<em>string</em>) Cookie 键名.</li>
        <li>value   - (<em>string</em>) 键名对应的值. 不能包含分号.</li>
        <li>options - (<em>mixed</em>, 可选) 另参考 <a href="#Cookie">Cookie</a>.</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>object</em>) 包含选项, 键名以及对应值的队形. 可用作 Cookie.remove 的第一个参数.</li>
      </ul>
      <h3>示例: </h3>
      <p>保存一个过期时长同 Session 的 Cookie:</p>
      <pre class="javascript"><span class="kw2">var</span> myCookie = Cookie.<span class="kw1">write</span><span class="br0">(</span><span class="st0">'username'</span>, <span class="st0">'Harald'</span><span class="br0">)</span>;

</pre>
      <p>将 Cookie 保存 1 天:</p>
      <pre class="javascript"><span class="kw2">var</span> myCookie  = Cookie.<span class="kw1">write</span><span class="br0">(</span><span class="st0">'username'</span>, <span class="st0">'JackBauer'</span>, <span class="br0">{</span>duration: <span class="nu0">1</span><span class="br0">}</span><span class="br0">)</span>;

</pre>
      <h2 id="Cookie:read"><a href="#Cookie:read">Cookie 方法: read</a></h2>
      <p class="description">读取 Cookie 值.</p>
      <h3>语法: </h3>
      <pre class="javascript"><span class="kw2">var</span> myCookie = Cookie.<span class="me1">read</span><span class="br0">(</span><span class="kw3">name</span><span class="br0">)</span>;

</pre>
      <h3>参数: </h3>
      <ol>
        <li>name - (<em>string</em>) 要获取的 Cookie 键名.</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>mixed</em>) 对应的值, 若不存在则返回 null.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript">Cookie.<span class="me1">read</span><span class="br0">(</span><span class="st0">'username'</span><span class="br0">)</span>;
</pre>
      <h2 id="Cookie:dispose"><a href="#Cookie:dispose">Cookie 方法: dispose</a></h2>
      <p class="description">从浏览器中移除 Cookie.</p>
      <h3>语法: </h3>
      <pre class="javascript"><span class="kw2">var</span> oldCookie = Cookie.<span class="me1">dispose</span><span class="br0">(</span>cookie<span class="br0">[</span>, options<span class="br0">]</span><span class="br0">)</span>;
</pre>
      <h3>参数: </h3>
      <ol>
        <li>name  - (<em>string</em>) 将被移除的 Cookie 的键名, 或之前保存的 Cookie 实例.</li>
        <li>options - (<em>object</em>, 可选) 另参考 <a href="#Cookie">Cookie</a>.</li>
      </ol>
      <h3>示例: </h3>
      <p>移除 Cookie:</p>
      <pre class="javascript">Cookie.<span class="me1">dispose</span><span class="br0">(</span><span class="st0">'username'</span><span class="br0">)</span>; <span class="co1">//Bye-bye JackBauer! Seeya in 24 Hours.</span>
</pre>
      <p>创建一个 Cookie 然后立即移除:</p>
      <pre class="javascript"><span class="kw2">var</span> myCookie = Cookie.<span class="kw1">write</span><span class="br0">(</span><span class="st0">'username'</span>, <span class="st0">'Aaron'</span>, <span class="br0">{</span>domain: <span class="st0">'mootools.net'</span><span class="br0">}</span><span class="br0">)</span>;
<span class="kw1">if</span> <span class="br0">(</span>Cookie.<span class="me1">read</span><span class="br0">(</span><span class="st0">'username'</span><span class="br0">)</span> == <span class="st0">'Aaron'</span><span class="br0">)</span> <span class="br0">{</span> myCookie.<span class="me1">dispose</span><span class="br0">(</span><span class="br0">)</span>; <span class="br0">}</span>

</pre>
    </div>
  </div>
<script type="text/javascript" src="../../../assets/mootools-core-1.3.js"></script>
<script type="text/javascript" src="../../../assets/mootools-more-smoothscroll.js"></script>
<script type="text/javascript" src="../../../assets/doc-assist.js"></script>
</body>
</html>